package com.baosight.risk.common.quartz;

import com.baosight.risk.api.riri.IRiriRiskService;
import com.baosight.risk.common.utils.DateUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.quartz.SysJobParamMapper;
import com.baosight.risk.mapper.xtzy.XtzyCompanyMapper;
import com.baosight.risk.service.quartz.entity.SysJobParam;
import com.baosight.risk.service.xtzy.entity.XtzyCompany;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;


@Slf4j
@Component("riskRiRiTaskSub")
public class RiskRiRiTaskSub {
    @Autowired
    private IRiriRiskService ririRiskService;

    @Autowired
    private XtzyCompanyMapper companyMapper;

    @Autowired
    private SysJobParamMapper sysJobParamMapper;

    public void testParams(String params, String jobId) throws Exception {
        log.error("进入定时任务————各基地风险自评");
        String acctYear = "";
        String quarterCurrent = "";
        String manageCompCode = "";
        String manageCompName = "";
        String timingTaskId="";
        if (StringUtils.isNotEmpty(params) && params.indexOf(",") > 0) {
            String[] split = params.split(",");
            acctYear = split[0];
            quarterCurrent = split[1];
            manageCompCode = split[2];
            manageCompName = split[3];
            timingTaskId=split[4];
        }

        List<String> manageCompCodeList = new ArrayList<String>();
        List<String> manageCompNameList = new ArrayList<String>();
        List<XtzyCompany> companyList=new ArrayList<XtzyCompany>();
        if(StringUtils.isNotEmpty(manageCompCode)){
            String [] manageCompCodeData=manageCompCode.split("\\\\");
            String [] manageCompNameData=manageCompName.split("\\\\");
            manageCompCodeList= Arrays.asList(manageCompCodeData);
            manageCompNameList= Arrays.asList(manageCompNameData);
        }
        if(StringUtils.isEmpty(manageCompCode)){
            SysJobParam param=new SysJobParam();
            param.setJobGuid(timingTaskId);
            List<SysJobParam> jobParamList= sysJobParamMapper.select(param);
            if (StringUtils.isEmpty(manageCompCode) && jobParamList.size()>0) {
                String [] manageCompCodeData=manageCompCode.split("\\\\");
                manageCompCodeList= Arrays.asList(manageCompCodeData);;
            }
            if (StringUtils.isEmpty(manageCompName)) {
                String [] manageCompNameData=manageCompName.split("\\\\");
                manageCompNameList= Arrays.asList(manageCompNameData);
            }
        }
        if (StringUtils.isEmpty(acctYear)) {
            acctYear = DateUtils.format(new Date(), "yyyy");
        }
        if (StringUtils.isEmpty(quarterCurrent)) {
            quarterCurrent = DateUtils.getQuarter(new Date());
        }
        for (int i=0;i<manageCompCodeList.size();i++){
            Map paramMap=new HashMap();
            paramMap.put("acctYear",acctYear);
            paramMap.put("quarterCurrent",quarterCurrent);
            paramMap.put("manageCompCode", manageCompCodeList.get(i));
            paramMap.put("manageCompName", manageCompNameList.get(i));
            XtzyCompany companyParam=new XtzyCompany();
            companyParam.setPathLevel((short)3);
            companyParam.setPathCode(manageCompCodeList.get(i));
            companyList = companyMapper.getRiskCompany(companyParam);
            ririRiskService.saveTimingTask(paramMap, companyList,jobId);
        }

        log.error("执行结束————各基地风险自评");
    }

    public void testNoParams() {
        log.error("执行无参方法");
    }
}